import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;

import javax.imageio.ImageIO;

public class IMGtoRGB24 {
	public static void main(String[] args) throws Exception {
		if (args.length < 2)
			throw new Exception(
					"Usage: java IMGtoRGB24 image_file_name output_file_name");
		String image_file_name = args[0];
		String output_file_name = args[1];
		if (new File(image_file_name).exists() == false)
			throw new Exception("File " + image_file_name + " not found.");
		BufferedImage image = ImageIO.read(new File(image_file_name));
		if (image.getWidth() < 240 || image.getHeight() < 320)
			throw new Exception("Width<240 or Height<320");
		int x, y;
		int a, r, g, b;
		int p;
		FileOutputStream output = new FileOutputStream(output_file_name);
		for (y = 0; y < 320; y++)
			for (x = 0; x < 240; x++) {
				p = image.getRGB(x, y);
				a = (p >>> 24) & 0xFF;
				if (a != 255)
					throw new Exception("alpha != 255");
				r = (p >>> 16) & 0xFF;
				g = (p >>> 8) & 0xFF;
				b = (p) & 0xFF;
				output.write(r);
				output.write(g);
				output.write(b);
			}
		output.close();
	}
}
